<template>
  <!-- 一条文章单元格 -->
  <van-cell   @click="$router.push(`/article?art_id=${artObj.art_id}`)">
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{artObj.title}}</span>
         <!-- 单图 -->
        <img class="thumb" v-lazy="artObj.cover.images[0]" v-if="artObj.cover.type === 1"    />
      </div>
        <!-- 三张图片 -->
    <div class="thumb-box" v-if="artObj.cover.type > 1"  >
    <img class="thumb"     v-for="(imgUrl, index) in artObj.cover.images"   :key="index"
        v-lazy="imgUrl" />
    <!-- <img class="thumb" src="https://img01.yzcdn.cn/vant/cat.jpeg" /> -->
    <!-- <img class="thumb" src="https://img01.yzcdn.cn/vant/cat.jpeg" /> -->
  </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <!-- 作者 -->
          <span>{{artObj.aut_name}}</span>
          <!-- 0评论 -->
          <span>{{artObj.comm_count}}</span>
          <!-- 8个月前 -->
          <span>{{$timeAgo( artObj.pubdate)}}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="showActionSheetFn"   v-if='showCross'    />
          <!-- 一级反馈面板 -->
    <van-action-sheet v-model="show1" :actions="FIRST_ACTIONS" @select="onSelectFn" get-container="body" cancel-text="取消"/>
    <van-action-sheet v-model="show2" :actions="SECOND_ACTIONS" @select="onSelectFn" get-container="body" cancel-text="取消"/>
      </div>
    </template>
  </van-cell>
</template>

<script>
import { dislikeArticleAPI, reportArticleAPI } from '@/api/home'
import {
  FIRST_ACTIONS,
  SECOND_ACTIONS
} from '@/constant'
export default {
  name: 'ArticleItem',
  props: {
    // 父传子 文章对象
    artObj: {
      type: Object,
      required: true
    },
    // 控制×好
    showCross: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show2: false,
      show1: false,
      FIRST_ACTIONS,
      SECOND_ACTIONS
    }
  },
  methods: {
    // 反馈面板点击事件
    async  onSelectFn ({ id, name }) {
      // 展示动作面板
      if (id === 11) {
        await dislikeArticleAPI({
          artId: this.artObj.art_id
        })
        this.$emit('dislike')
      // action绑定的数据对象
      } else if (id === 22) { // 选择的是反馈垃圾内容
        // 证明点击的是反馈垃圾内容
        this.show2 = true
      } else {
        // 这里就是二级反馈
        try {
          await reportArticleAPI({
            target: this.artObj.art_id,
            type: id,
            remark: !id ? '其它问题' : name
          })
          this.show2 = this.show1 = false
          this.$notify({ type: 'success', message: '举报成功' })
        } catch (err) {
          this.$notify({ type: 'warning', message: '举报失败' })
        }
      }
    },
    // 点击了叉号按钮
    showActionSheetFn () {
      // 展示动作面板
      this.show1 = true
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
  margin: 0 3px;
  &:first-child{
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
